package gov.kqqzyyy.monkey.orderpad.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.socks.library.KLog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.orderpad.R;
import gov.kqqzyyy.monkey.orderpad.adapter.PatientJRHCAdapter;
import gov.kqqzyyy.monkey.orderpad.network.RetrofitUtil;
import gov.kqqzyyy.monkey.orderpad.network.base.BaseObserver;
import gov.kqqzyyy.monkey.orderpad.network.bean.BaseResponse;
import gov.kqqzyyy.monkey.orderpad.object.Patient_JRHC;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Monkey on 2018/3/9.
 * 今日换床病人
 */

@SuppressLint("NonConstantResourceId")
public class PatientJRHCActivity extends NavigationBaseActivity {

    @BindView(R.id.container)
    FrameLayout container;

    ListView mPatientListView;

    private BaseObserver<List<Patient_JRHC>> hcbrObserver;

    private PatientJRHCAdapter adapter;
    private final Handler myHandler = new Handler();
    private final Runnable mLoadingRunnable = this::loadView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getWindow().getDecorView().post(() -> myHandler.post(mLoadingRunnable));
    }

    private void loadView() {
        container.removeAllViews();
        LayoutInflater.from(this).inflate(R.layout.activity_patient_jrhc, container);
        setActionBarTitle("今日换床病人");
        onBindUI();
        adapter = new PatientJRHCAdapter(this);
        mPatientListView.setAdapter(adapter);
        doGetJRHCPatient();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (hcbrObserver != null) {
            hcbrObserver.doCancelRequest();
        }
    }

    private void onBindUI() {
        mPatientListView = findViewById(R.id.patient_jrhc_listview);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            backToOrderMenu();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void backToOrderMenu() {
        Intent intent = new Intent(getApplicationContext(), OrderMenuActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
        finish();
    }

    private void doGetJRHCPatient() {
        KLog.d("APE","获取换床病人信息");
        showProgressDialog("获取换床病人信息中");
        Observable<BaseResponse<List<Patient_JRHC>>> observable = RetrofitUtil.getInstence().getHCBR();
        hcbrObserver = new BaseObserver<List<Patient_JRHC>>() {
            @Override
            protected void onSuccees(BaseResponse<List<Patient_JRHC>> t) {
                cancelProgressDialog();
                KLog.d("APE", t.getData());
                adapter.setList(t.getData());
            }

            @Override
            protected void onFailure(Throwable e, boolean isNetWorkError) {
                cancelProgressDialog();
                showToast("获取换床病人数据失败：" + e.getMessage());
            }

            @Override
            protected void onCodeError(BaseResponse<List<Patient_JRHC>> t) {
                super.onCodeError(t);
                cancelProgressDialog();
                showToast("获取换床病人数据失败\n错误代码：" + t.getCode() + "\n错误信息：" + t.getMsg());
            }

        };
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(hcbrObserver);

    }

}
